package com.example.crowdfunding.mvc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * The type Web app security config.
 */
@Configuration // 声明本类是spring配置类
@EnableWebSecurity // 开启spring security的web相关功能
@EnableGlobalMethodSecurity(prePostEnabled = true) // 开启注解
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * The User details service.
     */
    @Resource
    UserDetailsService userDetailsService;

    /**
     * 加密类（盐值）
     *
     * @return the b crypt password encoder
     */
    @Bean
    public BCryptPasswordEncoder getPasswordEncoder() {
        // 此处不会真的每次都去new，其实还是单例模式
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder builder) throws Exception {
        /*
            取消内存登录
            builder.inMemoryAuthentication()
                    .withUser("hanj").password("123")
                    .authorities("save");
        */

        builder
                .userDetailsService(userDetailsService) // 验证信息
                .passwordEncoder(getPasswordEncoder()) //
        ;
    }

    @Override
    protected void configure(HttpSecurity security) throws Exception {

        security
                .authorizeRequests()
                .antMatchers("/bootstrap/**").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/fonts/**").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/jquery/**").permitAll()
                .antMatchers("/layer/**").permitAll()
                .antMatchers("/script/**").permitAll()
                .antMatchers("/ztree/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/admin/to/login.html")
                .permitAll()
                .loginProcessingUrl("/admin/security/login.html")
                .permitAll()
                .usernameParameter("loginAccount")
                .passwordParameter("userPwd")
                .defaultSuccessUrl("/admin/to/main-page.html")
                .and()
                .logout()
                .logoutUrl("/admin/security/logout.html")
                .logoutSuccessUrl("/admin/to/login.html")
                .and()
                .csrf()
                .disable();
    }

}
